package com.isoftstone.demo.workflow.controller;

import java.io.InputStream;
import java.util.Map;

import javax.servlet.http.HttpServletResponse;

import org.activiti.engine.ActivitiException;
import org.activiti.engine.repository.Model;
import org.apache.commons.io.IOUtils;
import org.springframework.data.domain.Pageable;
import org.springframework.data.web.PageableDefault;
import org.springframework.util.MultiValueMap;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.servlet.mvc.support.RedirectAttributes;

import com.alibaba.fastjson.JSONObject;
import com.fasterxml.jackson.databind.node.ObjectNode;
import com.isoftstone.demo.workflow.service.WorkflowService;
import com.isoftstone.services.base.RespBaseController;

/** 
 * <br> 工作流Controller
 * @author  majintao
 * @version  [版本号, 2017年11月24日]
 * @see  [相关类/方法]
 * @since  [设备信息/设备数据信息]
 */
@RestController
@RequestMapping("/act")
public class WorkflowController extends RespBaseController<WorkflowService>{

	@Override
	public Long getModuleId() {
		// TODO Auto-generated method stub
		return null;
	}
	
	/**
	 * 流程模型列表
	 */
	@PostMapping("/list")
	public String modelList(String category,@PageableDefault Pageable pageable) {
		Map<String,Object> result = service.getModelList(category, pageable);
		return toJson(result);
	}
	
	/**
	 * 新建工作流模板
	 * @param json
	 * @throws Exception
	 */
	@PostMapping("/create")
	public String create(@RequestBody JSONObject json){
		try{
			String name = json.getString("name");
			String key = json.getString("key");
			String description = json.getString("description");
			String category = json.getString("category");
			Model model = service.Creat(name,key,description,category,getCurrentUser());
			return toJson(model);
		}catch(Exception e){
			e.printStackTrace();
			log.error("创建模型失败：", e);
			return null;
		}
	}

	@GetMapping("/service/model/{modelId}/json")
	public ObjectNode getEditorJson(@PathVariable String modelId){
		return service.getEditorJson(modelId);
	}
	
	
	@GetMapping("/service/editor/stencilset")
	public @ResponseBody String getStencilset() {
		InputStream stencilsetStream = this.getClass().getClassLoader().getResourceAsStream("stencilset.json");
		try {
			return IOUtils.toString(stencilsetStream, "utf-8");
		} catch (Exception e) {
			throw new ActivitiException("Error while loading stencil set", e);
		}
	}
	
	@PostMapping("/service/model/{modelId}/save")
	public void saveEditorJson(@PathVariable String modelId, @RequestBody MultiValueMap<String, String> values){
		try {
		service.saveEditorJson(modelId, values);
		} catch (Exception e) {
			e.printStackTrace();
			throw new ActivitiException("Error saving model", e);
		}
	}
	
	/**
	 * 导出model的xml文件
	 */
	@GetMapping("/export/{modelId}")
	public void export(@PathVariable String modelId,HttpServletResponse response) {
		service.export(modelId,response);
	}
	
	/**
	 * 导出model的xml文件
	 * @throws Exception 
	 */
	@GetMapping("/delete/{modelId}")
	public String delete(@PathVariable String modelId) throws Exception {
		Operation operation =
	            new Operation("workflow.model.delete").setMsgArgs(modelId);
		return respSuccess(() -> service.delete(modelId), operation);
		
	}
	
	/**
	 * 根据Model部署流程
	 * @throws Exception 
	 */
	@GetMapping("/deploy/{modelId}")
	public String deploy(@PathVariable String modelId) throws Exception {
		Operation operation =
	            new Operation("workflow.model.deploy").setMsgArgs(modelId);
		return respSuccess(() -> service.deploy(modelId), operation);
	}
}
